package com.cqtd.demo.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqtd.base.pojo.FooGenericSearch;
import com.cqtd.base.pojo.FooGenericSearchResult;
import com.cqtd.base.service.FooGenericService;
import com.cqtd.demo.model.Demo;
import com.cqtd.role.model.Role;
import com.cqtd.util.FooUtils;
import com.cqtd.util.StaticMethod;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/demo/demoAction")
public class DemoAction {
	private static final Logger logger = Logger.getLogger(DemoAction.class);
	@Autowired
	private FooGenericService fooGenericService;

	@RequestMapping("/demolist")
	@ResponseBody
	public void demolist(HttpServletRequest request,HttpServletResponse response) {

		String hql = "from Demo where deleted='0'";
		String countHql = "select count(id) " + hql;

		
		//http://localhost:8080/cqtd/demo/demoAction/demolist.action?_dc=1367746196823&page=1&start=0&limit=15

		int rows = StaticMethod.nullObject2int(request.getParameter("limit"));
		int page = StaticMethod.nullObject2int(request.getParameter("page"));
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		fooGenericSearch.setPage(page);
		fooGenericSearch.setRows(rows);
		fooGenericSearch.setCountHql(countHql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService
				.searchAndCount(fooGenericSearch);
		Map<String, Object> map = Maps.newHashMap();
		map.put("contatos", fooGenericSearchResult.getResult());
		map.put("total", fooGenericSearchResult.getTotalCount());
		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(HttpServletResponse response, Demo demo) {
		demo.setDeleted("0");
		fooGenericService.save(demo);
		try {
			FooUtils.printJsonSuccessMsg(response);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/remove")
	@ResponseBody
	public void remove(HttpServletRequest request,HttpServletResponse response) {
		String ids = StaticMethod.nullObject2String(request.getParameter("ids"));
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		
		String iduse = StaticMethod.getDealIds(ids, ",");
		logger.info(iduse);
		String hql = "from Demo where deleted='0' and id in "+iduse;
		fooGenericSearch.setQueryHql(hql);
		FooGenericSearchResult myResult = fooGenericService.search(fooGenericSearch);
		logger.info("size="+myResult.getResult().size());
		for(Demo demo:(List<Demo>)myResult.getResult()){
			demo.setDeleted("1");
			fooGenericService.update(demo);
		}
		try {
			FooUtils.printJsonSuccessMsg(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/update")
	@ResponseBody
	public void update(HttpServletResponse response, Demo demo) {
		demo.setDeleted("0");
		fooGenericService.update(demo);
		try {
			FooUtils.printJsonSuccessMsg(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	@SuppressWarnings({ "unused", "unchecked" })
	@RequestMapping("/demolist2")
	@ResponseBody
	public void demolist2(HttpServletRequest request,
			HttpServletResponse response) {
		List<Role> result = Lists.newArrayList();
		String node = StaticMethod
				.nullObject2String(request.getParameter("id"));
		String hql = "from Demo where parentId=:parentId";
		Map<String, Object> params = new HashMap<String, Object>();
		if (node.equals("root") || "".equals(node)) {
			node = "";
		}
		params.put("parentId", node);
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		result = (List<Role>) fooGenericService.search(fooGenericSearch,params)
				.getResult();
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("models", result);

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
